package com.atguigu.eduService.api;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.commonutils.vo.CourseWebVoForOrder;
import com.atguigu.eduService.client.OrderClient;
import com.atguigu.eduService.entity.vo.ChapterVo;
import com.atguigu.eduService.entity.vo.CourseWebVo;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "课程前台显示")
@RestController
@RequestMapping("/eduService/courseapi")
@CrossOrigin
public class CourseApiController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "根据课程id查到课程相关信息跨模块调用")
    @GetMapping("getCourseInForOrder/{courseId}")
    public CourseWebVoForOrder getCourseInForOrder(@PathVariable("courseId") String courseId) {
        CourseWebVo courseWebVo = courseService.getCourseWebVo(courseId);
        CourseWebVoForOrder courseWebVoForOrder = new CourseWebVoForOrder();
        BeanUtils.copyProperties(courseWebVo, courseWebVoForOrder);
        return courseWebVoForOrder;
    }

    @ApiOperation(value = "更具课程id查询课程相关信息")
    @GetMapping("getCourseWebInfo/{courseId}")
    public R getCourseWebInfo(@PathVariable String courseId,
                              HttpServletRequest request) {
        CourseWebVo courseWebVo = courseService.getCourseWebVo(courseId);
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoById(courseId);
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBuyCourse = orderClient.isBuyCourse(courseId, memberId);
        return R.ok().data("courseWebVo", courseWebVo)
                .data("chapterVoList", chapterVoList)
                .data("isBuyCourse", isBuyCourse);
    }


}
